package music

import (
	"context"
	"crypto/md5"
	"fmt"
	"mime/multipart"
	"time"

	"go-weixin/app/music/musicapi/internal/svc"
	"go-weixin/app/music/musicapi/internal/types"
	"go-weixin/app/music/musicrpc/pb/proto/music"

	"github.com/zeromicro/go-zero/core/logx"
)

type MusicDeleteLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewMusicDeleteLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MusicDeleteLogic {
	return &MusicDeleteLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *MusicDeleteLogic) MusicDelete(req *types.MusicDeleteReq, file multipart.File, fileHeader *multipart.FileHeader) (resp *types.MusicDeleteResp, err error) {
	byteData := make([]byte, fileHeader.Size)
	file.Read(byteData)
	fileHash := fmt.Sprintf("%x", md5.Sum(byteData))

	ctx, canelFunc := context.WithTimeout(context.Background(), 5*time.Second)
	defer canelFunc()
	_, err = l.svcCtx.MusicRpc.MusicDelete(ctx, &music.MusicDeleteReq{
		Md5: fileHash,
	})
	if err != nil {
		return nil, err
	}
	return
}
